<script lang="ts" setup>
import { ref } from 'vue'
import { ElMessage } from 'element-plus'
import { GetData } from '@/api/modules/memberscore'

// const imgDomain = 'https://img1.eprhan.cc'

const findFrom = ref({
  UserId: '',
  Mobile: '',
  VipLevel: '',
  Time: [],
  TransNo: '',
})
const dataList: any = ref([])
const loading = ref(true)
const total = ref(0)
let excelTotal = 0
let PageSize = 20

// 获取数据
function GetDataList(PageIndex: number = 1, PageSize: number = 20) {
  const params = {
    PageIndex,
    PageSize,
    ...(findFrom.value.UserId && ({ UserId: findFrom.value.UserId })),
    ...(findFrom.value.Mobile && ({ Mobile: findFrom.value.Mobile })),
    ...(findFrom.value.VipLevel && ({ VipLevel: findFrom.value.VipLevel })),
    ...(findFrom.value.TransNo && ({ TransNo: findFrom.value.TransNo })),
    ...(findFrom.value.Time.length !== 0 && ({ StartAddTime: findFrom.value.Time[0], EndAddTime: findFrom.value.Time[1] })),
  }
  GetData(params).then((res: any) => {
    if (res.statusCode === 200) {
      dataList.value = res.data.items
      total.value = res.data.total
      if (total.value > excelTotal) {
        excelTotal = total.value
      }
      loading.value = false
    }
    else {
      ElMessage.error('获取数据失败')
      loading.value = false
    }
  })
}

// 查询
function Inquire() {
  GetDataList(1, PageSize)
}

// 翻页
function handleSizeChange(val: number) {
  GetDataList(1, val)
  PageSize = val
}

function handleCurrentChange(val: number) {
  GetDataList(val, PageSize)
}

onMounted(() => {
  GetDataList()
})
</script>

<template>
  <div>
    <PageMain>
      <SearchBar :show-toggle="false">
        <div class="header">
          <el-form label-positio="right" label-width="auto">
            <div class="from">
              <el-form-item label="用户ID">
                <el-input v-model="findFrom.UserId" placeholder="请输入用户ID" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="手机号码">
                <el-input v-model="findFrom.Mobile" placeholder="请输入手机号码" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="会员等级">
                <el-input v-model="findFrom.VipLevel" placeholder="请输入会员等级" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="单号">
                <el-input v-model="findFrom.TransNo" placeholder="请输入单号" clearable @keyup.enter="Inquire" />
              </el-form-item>
              <el-form-item label="加入时间">
                <el-date-picker
                  v-model="findFrom.Time"
                  clearable
                  type="datetimerange"
                  range-separator="到"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  value-format="YYYY-MM-DD HH:mm:ss"
                />
              </el-form-item>
            </div>
          </el-form>
          <div class="text-right">
            <el-button @click="Inquire">
              查询
            </el-button>
          </div>
        </div>
      </SearchBar>
      <ElDivider border-style="dashed" />
      <el-table v-loading="loading" :data="dataList" stripe style="width: 100%;" border>
        <el-table-column align="center" width="100" prop="id" label="ID" />
        <el-table-column align="center" prop="subscriber.userId" label="用户ID" width="120" />
        <el-table-column align="center" prop="subscriber" label="用户信息" width="200">
          <template #default="scope">
            <div v-if="!scope.row.subscriber">
              未授权
            </div>
            <div v-else class="user-name">
              <el-avatar :size="40" style="margin-right: 10px;" :src="scope.row.subscriber?.userAvatar" />
              <div>
                <p style="margin: 0;">
                  {{ scope.row.subscriber?.userNickName }}
                </p>
                <span>
                  {{ scope.row.subscriber?.userPhone }}
                </span>
              </div>
            </div>
          </template>
        </el-table-column>
        <el-table-column align="center" width="300" prop="subscriber.scOpenId" label="用户OpenID" />
        <el-table-column align="center" width="360" prop="uniqueNo" label="唯一编码" />
        <el-table-column align="center" width="140" prop="channelId" label="渠道">
          <template #default="scope">
            <el-tag>
              {{ scope.row.channelId === 1 ? '微信小程序'
                : scope.row.channelId === 5 ? '系统' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" width="120" prop="scoreCate" label="积分类别">
          <template #default="scope">
            <el-tag>
              {{ scope.row.scoreCate === 1 ? '收入'
                : scope.row.scoreCate === 2 ? '支出' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <!-- <el-table-column align="center" width="240" prop="scoreType" label="积分类型">
          <template #default="scope">
            <el-tag>
              {{ scope.row.scoreType === 1 ? '订单类'
                : scope.row.scoreType === 2 ? '活动类' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column> -->
        <el-table-column align="center" width="140" prop="score" label="积分" />
        <el-table-column header-align="center" width="240" prop="scoreDesc" label="积分描述" />
        <el-table-column align="center" width="300" prop="transNo" label="流水号" />
        <!-- <el-table-column align="center" width="200" prop="actionType" label="积分变动原因" /> -->
        <!-- <el-table-column align="center" width="200" prop="activityCode" label="活动代码" /> -->
        <el-table-column align="center" width="120" prop="scoreState" label="状态">
          <template #default="scope">
            <el-tag>
              {{ scope.row.scoreState === 1 ? '正常' : '未定义' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column align="center" width="200" prop="addTime" label="添加时间" />
        <el-table-column align="center" width="200" prop="updateTime" label="更新时间" />
      </el-table>
      <div style="height: 10px;" />
      <el-pagination
        :default-page-size="20"
        :page-sizes="[5, 10, 20, 50, 100]"
        layout="total, sizes, ->, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </PageMain>
  </div>
</template>

<style scoped>
.header {
  display: grid;
  grid-template-columns: 6fr 1fr;

  .from {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
    grid-gap: 20px;

    .el-form-item {
      margin-bottom: 0;
    }
  }
}

.user-name {
  display: flex;
  align-items: center;
  justify-content: left;
  margin-right: 25px;

  div {
    text-align: left;
  }
}
</style>
